package com.leef.createPay.service;

import com.leef.createPay.dao.one.JyxOrdersDetailsRepository;
import com.leef.createPay.dao.one.JyxOrdersPayRepository;
import com.leef.createPay.dao.one.JyxOrdersRepository;
import com.leef.createPay.dao.two.JyxLearningCourseRepository;
import com.leef.createPay.domain.one.JyxOrders;
import com.leef.createPay.domain.one.JyxOrdersDetail;
import com.leef.createPay.domain.one.JyxOrdersPay;
import com.leef.createPay.domain.two.JyxLearningCourse;
import com.leef.createPay.result.CommonCode;
import com.leef.createPay.result.response.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;


@Service
public class CreatePayService {
    @Autowired
    JyxOrdersPayRepository jyxOrdersPayRepository;
    @Autowired
    JyxOrdersRepository jyxOrdersRepository;
    @Autowired
    JyxOrdersDetailsRepository jyxOrdersDetailsRepository;
    @Autowired
    JyxLearningCourseRepository jyxLearningCourseRepository;

    @Transactional(transactionManager = "platformTransactionManagerOne")
    public PayResult createPay(String payCode) {
        //判断是否有订单
        JyxOrdersPay jyxOrdersPayByON = jyxOrdersPayRepository.findByOrderNumber(payCode);
        if(jyxOrdersPayByON != null && jyxOrdersPayByON.getStatus().equals("402001")){
            return new PayResult(CommonCode.FAIL, "订单已支付，请勿重复支付");
        }
        Optional<JyxOrders> optionalJyxOrders = jyxOrdersRepository.findById(payCode);
        if(!optionalJyxOrders.isPresent()){
            return new PayResult(CommonCode.FAIL, "支付失败！没有此订单");
        }
        JyxOrdersDetail jyxOrdersDetail = jyxOrdersDetailsRepository.findByOrderNumber(payCode);
        JyxOrders jyxOrders = optionalJyxOrders.get();
        if(jyxOrdersDetail==null){
            return new PayResult(CommonCode.FAIL,"支付失败！没有此订单");
        }
        //判断是否选课
        JyxLearningCourse byCourseId = jyxLearningCourseRepository.findByCourseIdAndUserId(jyxOrdersDetail.getItemId(),jyxOrders.getUserId());
        if(byCourseId != null && !StringUtils.isEmpty(byCourseId.getCourseId())&&!StringUtils.isEmpty(byCourseId.getUserId())){
            return new PayResult(CommonCode.SUCCESS, "订单已支付，请勿重复支付");
        }

        // courseId userid valid status

        Date date = new Date();
        long time = date.getTime();
        Date endTime = jyxOrders.getEndTime();
        long orderEndTime = endTime.getTime();
        if(orderEndTime < time){
            return new PayResult(CommonCode.FAIL,"订单超时，请重新支付");
        }
        JyxOrdersPay jyxOrdersPay = new JyxOrdersPay();
        jyxOrdersPay.setOrderNumber(payCode);
        String payNum = jyxOrdersDetail.getItemNum().toString();
        jyxOrdersPay.setPayNumber(payNum);
        jyxOrdersPay.setStatus("401002");
        jyxOrdersPayRepository.save(jyxOrdersPay);
        JyxLearningCourse jyxLearningCourse = new JyxLearningCourse();
        jyxLearningCourse.setUserId(jyxOrders.getUserId());
        jyxLearningCourse.setCourseId(jyxOrdersDetail.getItemId());
        jyxLearningCourse.setStatus("501001");
        jyxLearningCourseRepository.save(jyxLearningCourse);

        return new PayResult(CommonCode.SUCCESS, "支付成功！");
    }
}
